package liangtWorkSpace.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 找出所有 长度>=5的字符串，并且忽略大小写、去除重复字符串，然后按字母排序，最后用“爱心❤”连接成一个字符串输出！
 *
 * @ClassName NotForEach
 * @Description : for循环转为Lamdba
 * @Author : LiangT
 * @Date : 2020/6/18 13:08
 */
public class NotForEach {
    private static String head = "❤";
    private static ArrayList<String> strings = new ArrayList();

    static {
        strings.addAll(Arrays.asList("1", "2", "bilibili", "of", "codesheep", "5", "at", "BILIBILI", "codesheep", "23", "CHEERS", "6"));
    }

    /**
     * 常规For循环实现
     *
     * @param strS
     * @return
     */
    public static String getStr(ArrayList<String> strS) {
        if (strS == null) {
            return "";
        }
        StringBuffer str = new StringBuffer();
        for (String string : strS) {
            string = string.toLowerCase();//转小写
            if (string != null && string.length() > 4 && str.indexOf(string) < 0) {
                if (str.length() > 0) {
                    str.append(head);
                }
                str.append(string);
            }
        }
        return str.toString();
    }

    public static Boolean isNum(String str) {
        for
        (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String getLamdba(String[] strS) {
        String result = strings.stream()
                .filter(x -> x.length() > 4)        //字符串大于4
                .filter(x -> !isNum(x))             //排除数字
                .map(x->x.toLowerCase())            //赛选的全部小写
                .distinct()                         //去重操作
                .sorted(Comparator.naturalOrder())  //排序
                .collect(Collectors.joining(head)); // 连词造句
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getStr(strings));
    }
}
